package com.songshuai.sales.tool.service.impl;

import com.songshuai.sales.tool.dto.response.sop.ArticleSopResDto;
import com.songshuai.webapi.baseapi.constant.BaseConstant;
import com.songshuai.webapi.baseapi.constant.miniprogram.CacheKeys;
import com.songshuai.webapi.baseapi.dto.feign.response.phpsop.SopDto;
import com.songshuai.webapi.baseapi.dto.pangu.request.phpsop.PhpSopReqDto;
import com.songshuai.sales.tool.feign.PanguApiClient;
import com.songshuai.sales.tool.service.PhpSopService;
import com.songshuai.webapi.baseapi.enumerations.ResultStatus;
import com.songshuai.webapi.baseapi.pojo.QueryPageRes;
import com.songshuai.webapi.baseapi.pojo.QueryRes;
import com.songshuai.webapi.baseapi.pojo.QueryResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Slf4j
@Service
public class PhpSopServiceImpl extends BaseServiceImpl implements PhpSopService {

    @Autowired
    private PanguApiClient panguApiClient;

    @Override
    public QueryRes queryPhpSopTypeList(String type) {
        QueryResponse queryResponse = panguApiClient.getPhpSopTypeList(type);
        if(null != queryResponse && ResultStatus.SUCCESS.getErrorCode() == queryResponse.getCode()){
            Object obj = queryResponse.getData();
            return QueryRes.setResData(obj);
        }
        return QueryRes.setResData(null, ResultStatus.ERROR_SEARCH_FAILED);
    }

    @Override
    public QueryRes queryPhpSopList(PhpSopReqDto sopReqDto) {
        QueryResponse<QueryPageRes<List<SopDto>>> queryResponse = panguApiClient.getPhpSopList(sopReqDto);
        if(null != queryResponse && ResultStatus.SUCCESS.getErrorCode() == queryResponse.getCode()){
            QueryPageRes<List<SopDto>>  queryPageRes= queryResponse.getData();
            if(null != queryPageRes && !CollectionUtils.isEmpty(queryPageRes.getBody())){
                for(SopDto sopDto : queryPageRes.getBody()){
                    String useNumKey = CacheKeys.getPhpSopUseNum(sopDto.getId());
                    Integer useNum = 0;
                    if(redisUtil.hasKey(useNumKey)){
                        useNum = Integer.valueOf(redisUtil.getString(useNumKey));
                    }
                    sopDto.setUseNum(useNum);
                }
            }
            return QueryRes.setResData(queryPageRes);
        }
        return QueryRes.setResData(null, ResultStatus.ERROR_SEARCH_FAILED);
    }

    @Override
    public QueryRes queryPhpSopDetail(String id) {
        QueryResponse queryResponse = panguApiClient.getPhpSopDetail(id);
        if(null != queryResponse && ResultStatus.SUCCESS.getErrorCode() == queryResponse.getCode()){
            Object obj = queryResponse.getData();
            ArticleSopResDto sop = new ArticleSopResDto();
            if(null != obj){
                SopDto sopDto = (SopDto) obj;
                sop.setTitle(sopDto.getTitle());
                if("normal".equals(sopDto.getAttr())){
                    sop.setSopType(BaseConstant.SOP_TYPE_NORMAL);
                }else{
                    sop.setSopType(BaseConstant.SOP_TYPE_VIDEO);
                }
                sop.setSopContent(sopDto.getContent());
                sop.setImg(sopDto.getImage());
                sop.setDescription(sopDto.getRemark());
            }
            return QueryRes.setResData(sop);
        }
        return QueryRes.setResData(null, ResultStatus.ERROR_SEARCH_FAILED);
    }
}
